package org.droid.campusorder.adapter;

import java.io.File;
import org.droid.R;
import org.droid.campusorder.common.GlobalData;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CanteenShopAdapter extends BaseAdapter {

	private Hodle hodle;
	private Context mContext;
	private String[] canteenShopLogoPaths = null;// 食堂内容资源（图片）

	public CanteenShopAdapter(Context context) {
		this.mContext = context;
	}

	public String[] getCanteenShopLogoPaths() {
		return canteenShopLogoPaths;
	}

	public void setCanteenShopLogoPaths(String[] canteenShopLogoPaths) {
		this.canteenShopLogoPaths = canteenShopLogoPaths;
	}
	
	@Override
	public int getCount() {
		if (canteenShopLogoPaths != null) {
			return canteenShopLogoPaths.length;
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return canteenShopLogoPaths[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 定义一个ImageView,显示在GridView里
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView =inflater.inflate(R.layout.canteenshop_adpter, null);
			hodle =new Hodle();
			hodle.imageView =(ImageView) convertView.findViewById(R.id.iv_csadpter_griditem);
			hodle.textView = (TextView) convertView.findViewById(R.id.tv_csadpter_griditem);
//			hodle.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			hodle.imageView.setPadding(4, 4, 4, 10);
			convertView.setTag(hodle);
		} else {
			hodle = (Hodle) convertView.getTag();
		}
		File f = new File(canteenShopLogoPaths[position].trim());
		if (!f.exists()) {
			hodle.imageView.setImageResource(R.drawable.icon);
		} else {
			Bitmap btm = BitmapFactory
					.decodeFile(canteenShopLogoPaths[position].trim());
			hodle.imageView.setImageBitmap(btm);
			btm = null;
		}
		hodle.textView.setText(GlobalData.shopsBeanList.get(position).getName());
		convertView.setBackgroundResource(R.drawable.shoppic_bg);
		return convertView;
	}
	
	class Hodle{
		ImageView imageView;
		TextView textView;
	}
	

}
